package cdu.notFound.controller;

import cdu.notFound.model.Goods;
import cdu.notFound.model.Order;
import cdu.notFound.model.User;
import cdu.notFound.service.OrderService;
import cdu.notFound.service.ShopCartService;
import cdu.notFound.service.impl.OrderServiceImpl;
import cdu.notFound.service.impl.ShopCartServiceImpl;
import cdu.notFound.utils.LoginVerify;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *购买购物车的所有商品
 */
@WebServlet("/BuyAllShopcartServlet")
public class BuyAllShopcartServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter resOut = response.getWriter();
        Boolean isLogined = LoginVerify.isLogin(request);
        User user = null;
        if (isLogined) {
            user = (User) request.getSession().getAttribute("loginUser");
        } else {
            request.getRequestDispatcher("user/login.jsp?login-info=" + java.net.URLEncoder.encode("你应该先登录", "UTF-8")).forward(request, response);
        }
        ShopCartService shopCartService = new ShopCartServiceImpl();
        List<Goods> goods = null;
        goods = shopCartService.findGoodsByUser(user);
        if (goods != null && goods.size() != 0) {
            OrderService orderService = new OrderServiceImpl();
            Order order = new Order();
            Date date = new Date();
            //使用两个list来收集失败和成功的物品
            List<Goods> listSuc = new ArrayList<Goods>();
            List<Goods> listErr = new ArrayList<Goods>();
            for (Goods good : goods) {
                order.setGoodsId(good.getId());
                order.setUserId(user.getId());
                order.setMessage("此物品通过购物车批量购买");
                order.setDate(date);
                if (orderService.doCreate(order)) {
                    listSuc.add(good);
                    //购买成功则移除
                    shopCartService.removeList(good.getId(), user.getId());
                } else {
                    listErr.add(good);
                }
                if (listSuc.size() > 0) {
                    resOut.println("<p>物品：</p><p>");
                    for (int i = 0; i < listSuc.size(); i++) {
                        resOut.println("<a href=\"goods/info.jsp?goodsid=" + listSuc.get(i).getId() + "\">" + listSuc.get(i).getName() + "</a>　");
                    }
                    resOut.println("</p><p>购买成功!</p>");
                }
                if (listErr.size() > 0) {
                    resOut.println("<p>物品：</p><p>");
                    for (int i = 0; i < listErr.size(); i++) {
                        resOut.println("<a href=\"goods/info.jsp?goodsid=" + listErr.get(i).getId() + "\">" + listErr.get(i).getName() + "</a>　");
                    }
                    resOut.println("</p><p>购买失败</p>");
                }
            }
        }
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
